package _99练习;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

public class ColorFrame extends JFrame {

	private JPanel contentPane;
	
	Thread t1;
	
	boolean running;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ColorFrame frame = new ColorFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ColorFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		
		JButton btnNewButton = new JButton("ʼɫ");
		btnNewButton.setBounds(161, 38, 97, 23);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				t1 = new Thread(new Runnable() {

					@Override
					public void run() {
						running = true;
						
						while(running)
						{
											
							int r = (int)(Math.random()*256);
							int g = (int)(Math.random()*256);
							int b = (int)(Math.random()*256);
							Color c = new Color(r,g,b);
							
							contentPane.setBackground(c);
						}
						
					}

					
					
					
					
				});
				
				t1.start();
			}
		});
		contentPane.setLayout(null);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("ֹͣɫ");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//t1.stop();
				
				//t1.suspend();
				
				running = false;
			}
		});
		btnNewButton_1.setBounds(161, 107, 97, 23);
		contentPane.add(btnNewButton_1);
	}
}